/**
 * VYPe11 Project
 * VYPe11 language compiler.
 * Authors: Jozef Lang (xlangj01), 
 * 			Zoltan Zemko (xzemko01)
 * 2011
 */
package cz.vutbr.fit.VYPe.team18.VYPe11.nodes.statement;

import java.io.IOException;

import cz.vutbr.fit.VYPe.team18.VYPe11.nodes.expression.ExpressionNode;

public class IfElseStatementNode extends StatementNode {
	private ExpressionNode expression;
	private StatementListNode ifStatements;
	private StatementListNode elseStatements;
	
	public IfElseStatementNode(ExpressionNode expr,
								StatementListNode ifStatements,
								StatementListNode elseStatements) {
		this.expression=expr;
		this.ifStatements=ifStatements;
		this.elseStatements=elseStatements;
	}

	public ExpressionNode getExpression() {
		return expression;
	}

	public StatementListNode getIfStatements() {
		return ifStatements;
	}

	public StatementListNode getElseStatements() {
		return elseStatements;
	}

	@Override
	public void generateTAC() throws IOException {
		int labIndex=tacCounter.getNextLabelIndex();			
		expression.generateTAC();									// expression
		int tIndex=tacCounter.getTermIndex()-1;				
		writeTAC("if", "_t"+tIndex, "", "L"+labIndex+"_true");		// if expression!=0 then goto L1_true 
		writeTAC("goto", "", "", "L"+labIndex+"_false");			// goto L1_false
		writeTAC("label", "L"+labIndex+"_true", "", "");				// L1_true:
		ifStatements.generateTAC();
		writeTAC("goto", "", "", "L"+labIndex+"_end");				// goto L1_end
		writeTAC("label", "L"+labIndex+"_false", "", "");			// L1_false:
		elseStatements.generateTAC();
		writeTAC("label", "L"+labIndex+"_end", "", "");				// L1_end:
	}
}
